package com.gewu.pm.ui.activity.sales;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.bean.TerminalContactBean;
import com.gewu.pm.other.IntentKey;
import com.gewu.pm.utils.TextStarUtils;
import com.gewu.pm.utils.eventbus.EventBean;
import com.gewu.pm.utils.eventbus.EventBusUtil;
import com.gewu.pm.utils.eventbus.EventCode;
import com.hjq.widget.view.ClearEditText;
import com.hjq.widget.view.SwitchButton;

/**
 * 新增/编辑终端联系人页面
 */
public final class AddContactActivity extends AppActivity {

    private TextView mContactNameLabel;
    private ClearEditText mContactNameEdit;
    private TextView mContactPhoneLabel;
    private ClearEditText mContactPhoneEdit;
    private ClearEditText mContactNoteEdit;
    private SwitchButton mPrimarySwitch;
    private TextView mConfirmButton;

    /** 是否为编辑模式 */
    private boolean mIsEditMode = false;

    /** 编辑的联系人数据 */
    private TerminalContactBean mEditContact;

    /** 编辑的联系人位置 */
    private int mEditPosition = -1;

    /** 类型：0-销售线索，1-终端 */
    private int mType = 0;

    /**
     * 启动新增联系人页面
     * @param type 0-销售线索，1-终端
     */
    public static void start(Context context, int type) {
        Intent intent = new Intent(context, AddContactActivity.class);
        intent.putExtra(IntentKey.TYPE, type);
        context.startActivity(intent);
    }

    /**
     * 启动编辑联系人页面
     * @param type 0-销售线索，1-终端
     */
    public static void startForEdit(Context context, TerminalContactBean contact, int position, int type) {
        Intent intent = new Intent(context, AddContactActivity.class);
        intent.putExtra(IntentKey.DATA, contact);
        intent.putExtra(IntentKey.INDEX, position);
        intent.putExtra(IntentKey.TYPE, type);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.add_terminal_contact_activity;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mContactNameLabel = findViewById(R.id.tv_contact_name_label);
        mContactNameEdit = findViewById(R.id.et_contact_name);
        mContactPhoneLabel = findViewById(R.id.tv_contact_phone_label);
        mContactPhoneEdit = findViewById(R.id.et_contact_phone);
        mContactNoteEdit = findViewById(R.id.et_contact_note);
        mPrimarySwitch = findViewById(R.id.sb_primary);
        mConfirmButton = findViewById(R.id.tv_bottom_button);

        // 设置必填标记
        TextStarUtils.setRequiredText(mContactNameLabel, getString(R.string.contact_name));
        TextStarUtils.setRequiredText(mContactPhoneLabel, getString(R.string.contact_phone));

        mPrimarySwitch.setColor(0xFFC90000,0xFFE5E5E5);

        // 设置点击事件
        setOnClickListener(mConfirmButton);
    }

    @Override
    protected void initData() {
        // 获取类型
        mType = getInt(IntentKey.TYPE, 0);

        // 判断是否为编辑模式
        mEditContact = (TerminalContactBean) getSerializable(IntentKey.DATA);
        mEditPosition = getInt(IntentKey.INDEX, -1);

        if (mEditContact != null && mEditPosition >= 0) {
            // 编辑模式
            mIsEditMode = true;

            // 根据类型设置标题
            if (mType == 1) {
                setTitle("编辑终端联系人");
            } else {
                setTitle("编辑线索联系人");
            }

            // 填充数据
            mContactNameEdit.setText(mEditContact.getName());
            mContactPhoneEdit.setText(mEditContact.getPhone());
            mContactNoteEdit.setText(mEditContact.getNote());
            mPrimarySwitch.setChecked(mEditContact.isPrimary());
        } else {
            // 新增模式
            mIsEditMode = false;

            // 根据类型设置标题
            if (mType == 1) {
                setTitle(R.string.add_terminal_contact);
            } else {
                setTitle("新增线索联系人");
            }
        }
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == mConfirmButton) {
            // 验证必填项
            String name = mContactNameEdit.getText().toString().trim();
            if (TextUtils.isEmpty(name)) {
                toast("请输入联系人姓名");
                return;
            }

            String phone = mContactPhoneEdit.getText().toString().trim();
            if (TextUtils.isEmpty(phone)) {
                toast("请输入联系电话");
                return;
            }

            String note = mContactNoteEdit.getText().toString().trim();
            boolean isPrimary = mPrimarySwitch.isChecked();

            // 创建联系人数据
            TerminalContactBean contact = new TerminalContactBean(name, phone, note, isPrimary);

            if (mIsEditMode) {
                // 编辑模式：发送编辑事件
                contact.setIndex(mEditPosition);
                EventBusUtil.sendEvent(new EventBean<>(EventCode.TERMINAL_CONTACT_EDIT, contact));
            } else {
                // 新增模式：发送新增事件
                EventBusUtil.sendEvent(new EventBean<>(EventCode.TERMINAL_CONTACT_ADD, contact));
            }

            // 关闭当前页面
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 释放资源
    }
}
